// pages/home/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    head: {}, // 头部
    swiper: {}, // 轮播图
    homeData: [], // 首页数据
    bgColor: '#629EFF',
    swiperIndex: 0,
    opacity: 1,
    autoplay: true, // 轮播图自动播放
    waterFall: [], // 猜你喜欢商品列表
    refresh: true,
    height: 0,
    page: 0, // 商品翻页
    count: 10, // 商品总数
    fixedTop: 0, // show Top 吸顶 hide
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._loadHomeData()
    this._loadShopData()
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function (e) {
    console.log(e);
  },

  // 轮播图切换改变背景色
  changeBgColor({detail}) {
    this.setData({
      swiperIndex: detail.current
    })
    this.setData({
      bgColor: this._getColor(this.data.swiper, detail.current)
    })
  },

  // 下拉刷新被触发
  handleFresher(e) {
    let dy = e.detail.dy
    let opacity = 1 - Math.min(dy / 60, 1)
    this.setData({
      height: dy - 40,
      opacity
    })
  }, 

  // 下拉刷新请求数据
  handleFresherRefresh(e) {
    let self = this
    this.setData({
      height: 0,
      waterFall: []
    })
    function fn () {
      setTimeout(() => {
        self.setData({
          refresh: false,
          opacity: 1,
        })
        wx.showToast({
          title: '刷新成功',
          icon: 'success',
        })
      }, 1500)
    }
    this._loadHomeData(fn)
  },

  // 下拉刷新被终止
  handleAbout(){
    this.setData({
      height: 0,
      opacity: 1
    })
  },

  // 触底翻页
  handleScrollToBottom(){
    this._loadShopData()
    wx.showLoading({
      title: '加载中',
    })
  },

  // scroll event
  handleScroll(e) {
    let top = e.detail.scrollTop
    if(top > 300 && this.data.bgColor==='#07a8b7') return
    this.setData({
      fixedTop: top > 64,
      autoplay: top < 210,
      bgColor: top > 210 ? '#07a8b7' : this._getColor(this.data.swiper, this.data.swiperIndex)
    })
  },

  // 加载商品  翻页
  _loadShopData () {
    if(this.data.page >= this.data.count) {
      wx.hideLoading()
      wx.showToast({
        title: '没有更多数据了！',
        icon: 'loading',
        duration: 1500
      })
      return
    }
    wx.request({
      url: 'https://vstore-api.viomi.com.cn/vstore-api/wares/open-api/v1/wares/query/pageRecommendSku?businessType=3&plateForm=1&limit=10&num='+ this.data.page +'&sourceChannel=13',
      success: (result) => {
        let guessDataList = result.data.mobBaseRes.result
        this.setData({
          page: this.data.page + 10,
          waterFall: guessDataList.prodSkuRecommendResps,
          count: guessDataList.count
        })
        wx.hideLoading()
      }
    })
  },

  // 请求首页数据
  _loadHomeData(fn) {
    wx.request({
      url: 'https://vstore-api.viomi.com.cn/vstore-api/layout/open-api/v1/mallLayout/showHomePageRefactor?navbarType=3&pageNum=1&pageSize=50&sourceChannel=13',
      method: 'GET',
      success: (res) => {
        let home = res.data.mobBaseRes.result.widgets
        this.setData({
          head: home[0],
          swiper: home[1],
          homeData: home,
          bgColor: this._getColor(home[1], 0)
        })
        fn ? fn() : ''
      }
    })
  },
  
  _getColor(obj, index) {
    return obj.children[index].attrs.bk_gradient_color.slice(2, -2)
  },

})